package com.clkj.tramcarlibrary.fragment.toTiyuguan;

import android.support.annotation.NonNull;
import android.util.Log;

import com.clkj.tramcarlibrary.entity.JinzhanDetail;
import com.clkj.tramcarlibrary.entity.ToTygShoumoDetail;
import com.clkj.tramcarlibrary.http.HttpService;
import com.clkj.tramcarlibrary.schedulers.BaseSchedulerProvider;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/2/14.
 */

public class ToTygPresenter implements ToTygContract.Presenter {

    @NonNull
    ToTygContract.View mToTygView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;

    public ToTygPresenter(@NonNull ToTygContract.View mToTygView, @NonNull BaseSchedulerProvider mSchedulerProvider, @NonNull HttpService mHttpService) {
        this.mToTygView = mToTygView;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mToTygView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }

    //获取进站信息
    @Override
    public void getToTygJinzhan() {
        mToTygView.showLoading();
        Subscription subscription = mHttpService.getToTygJinzhanDetail()
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mToTygView.dismissLoading();
                            mToTygView.showToTygJinzhanError("服务器获取进站信息失败，重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("失败", "失败");
                        mToTygView.dismissLoading();
                        mToTygView.showError("服务器获取数据异常");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Log.e("去体育馆方向站点详情", jsonObject.toString());
                        mToTygView.dismissLoading();
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();
                        List<JinzhanDetail> jinzhanDetails = new Gson().fromJson(jsonArray, new TypeToken<List<JinzhanDetail>>() {
                        }.getType());

                        mToTygView.showToTygJinzhan(jinzhanDetails);
                    }
                });
        mSubscriptions.add(subscription);
    }

    //获取首末班信息
    @Override
    public void getToTygShoumo() {
        mToTygView.showLoading();
        Subscription subscription = mHttpService.getToTygShoumoDetail()
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mToTygView.dismissLoading();
                            mToTygView.showToTygShoumoError("服务器首末班信息失败，重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("失败", "失败");

                        mToTygView.dismissLoading();
                        mToTygView.showError("服务器获取数据异常");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Log.e("去体育馆获取首末班信息", jsonObject.toString());
                   // 去体育馆获取首末班信息: {"status":"success","message":"获取成功","data":[{"station_name":"南门站","first_time_other":"06:00","last_time_other":"22:00"},{"station_name":"龙光阁站","first_time_other":"06:02","last_time_other":"22:02"},{"station_name":"东岳庙站","first_time_other":"06:05","last_time_other":"22:05"},{"station_name":"纪念馆站","first_time_other":"06:08","last_time_other":"22:08"},{"station_name":"华西路站","first_time_other":"06:10","last_time_other":"22:10"},{"station_name":"古末口站","first_time_other":"06:13","last_time_other":"22:13"},{"station_name":"礼字坝站","first_time_other":"06:16","last_time_other":"22:16"},{"station_name":"河下站","first_time_other":"06:18","last_time_other":"22:18"},{"station_name":"乌沙河站","first_time_other":"06:21","last_time_other":"22:21"},{"station_name":"森林公园站","first_time_other":"06:23","last_time_other":"22:23"},{"station_name":"大剧院站","first_time_other":"06:25","last_time_other":"22:25"},{"station_name":"板闸站","first_time_other":"06:27","last_time_other":"22:27"},{"station_name":"三亚路站","first_time_other":"05:32","last_time_other":"22:30"},{"station_name":"汕头路站","first_time_other":"05:35","last_time_other":"22:32"},{"station_name":"八亭桥站","first_time_other":"05:37","last_time_other":"22:34"},{"station_name":"厦门路站","first_time_other":"05:39","last_time_other":"22:36"},{"station_name":"深圳路站","first_time_other":"05:41","last_time_other":"22:38"},{"station_name":"政务中心站","first_time_other":"05:43","last_time_other":"22:40"},{"station_name":"水渡口站","first_time_other":"05:45","last_time_other":"22:43"},{"station_name":"老坝口站","first_time_other":"05:48","last_time_other":"22:45"},{"station_name":"运河广场站","first_time_other":"05:50","last_time_other":"22:48"},{"station_name":"淮海东路站","first_time_other":"05:53","last_time_other":"22:51"},{"station_name":"体育馆站","first_time_other":"--","last_time_other":"--"}]}

                        mToTygView.dismissLoading();
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();

                        List<ToTygShoumoDetail> shoumoDetails = new Gson().fromJson(jsonArray, new TypeToken<List<ToTygShoumoDetail>>() {
                        }.getType());

                        mToTygView.showToTygShoumo(shoumoDetails);

                    }
                });
        mSubscriptions.add(subscription);
    }
}
